package com.bf.hotpoint.repository.redis;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import com.bf.hotpoint.enums.RedisKeyEnum;

@Repository
public class CommonRedisRepository {
	
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	
	public void setOperationUsers(String uuid){
		redisTemplate.boundSetOps(RedisKeyEnum.OPERATION_USER.getKey()).add(uuid);
	}
	
	public void deleteTempUsers(){
		if(!redisTemplate.hasKey(RedisKeyEnum.OPERATION_USER.getKey()))
		{
			redisTemplate.delete(RedisKeyEnum.OPERATION_USER.getKey());
		}
	}
	
	public void deleteByKey(String key){
		if(!redisTemplate.hasKey(key))
		{
			redisTemplate.delete(key);
		}
	}
	
	public void deleteTempUserByKey(String uuid)
	{
		redisTemplate.boundSetOps(RedisKeyEnum.OPERATION_USER.getKey()).remove(uuid);
	}	
	
	public void setDataOfSet(String key,String value){
		redisTemplate.boundSetOps(key).add(value);
	}
	
	public long getLongByKey(String key)
	{
		return Long.valueOf(redisTemplate.boundValueOps(key).get().toString());
	}
	
	public Set<String> getSetByKey(String key)
	{
		Object valueObject = redisTemplate.boundValueOps(key).get();
		if(valueObject == null)
			return null;
		return (Set<String>)valueObject;
	}
}
